$(function() {
	$("li").click(function() {
		$("li").removeClass("ok");
		$(this).addClass("ok");
		$(".address_box .c_pic").text(" ");
		$(".address_box .ok .c_pic").text("寄送到");
		$(".address_box .c_add").text(" ");
		$(".address_box .ok .c_add").text("默认地址");
		$(".address_box .amend a").text(" ");
		$(".address_box .ok .amend a").text("修改本地址");
	});
});

$(function() {
	$(".freight").click(function() {
		var che = $(this).children("input").attr("checked");
		if(che == "checked") {
			$(".freight").css("background-image", "url(img/r7.png)");
		} else {
			$(".freight").css("background-image", "url(img/r8.png)")
		}
	});
});

$(function() {
	$(".label").click(function() {
		$(".label").removeClass("on");
		$(this).addClass("on");
	});
});


window.onload = function() {
	var use = document.getElementById("use");
	var core=document.getElementById("core");
	
	use.onmousemove=function(){
		core.style.display="block";
	}
	core.onmousemove=function(){
		core.style.display="block";
	}
	use.onmouseout=function(){
		core.style.display="none";
	}
	core.onmouseout=function(){
		core.style.display="none";
	}	
	
	var box = document.getElementById("address_box");
	var btn = box.getElementsByTagName("li");
	var box1 = document.getElementById("pay_box");
	var table = document.getElementById('cartTable');

	for(var i = 0; i < btn.length; i++) {
		btn[i].onclick = function() {
			var name = this.getElementsByTagName("span")[0].innerHTML;
			var name1 = this.getElementsByTagName("span")[1].innerHTML;
			var name2 = this.getElementsByTagName("span")[2].innerHTML;

			box1.getElementsByTagName("b")[0].innerHTML = "寄送至：" + name;
			box1.getElementsByTagName("b")[1].innerHTML = "收货人：" + name1 + " " + name2;
		}

	}

	var table = document.getElementById('cartTable'); // 购物车表格	
	var tr = table.children[0].rows; //行
	var priceTotal = document.getElementById('priceTotal'); //总计

	// 计算价格
	function getSubtotal(tr) {
		var cells = tr.cells;
		var price = cells[3] //单价		
		var subtotal = cells[6]; //小计td
		var countInput = tr.getElementsByTagName('input')[0]; //数目input
		//写入HTML

		subtotal.innerHTML = (parseInt(countInput.value) * parseFloat(price.getElementsByTagName('i')[0].innerHTML)).toFixed(2);
		priceTotal.innerHTML = "￥" + subtotal.innerHTML;
		priTotal.innerHTML = priceTotal.innerHTML;
	}
	//为每行元素添加事件
	for(var i = 0; i < tr.length; i++) {
//		将点击事件绑定到tr元素
				tr[i].onclick = function(e) {
					var e = e || window.event;
					var el = e.target || e.srcElement; //通过事件对象的target属性获取触发元素
					var cls = el.className; //触发元素的class
					var countInout = this.getElementsByTagName('input')[0]; // 数目input
					var value = parseInt(countInout.value); //数目
					//通过判断触发元素的class确定用户点击了哪个元素
					switch(cls) {
						case 'add': //点击了加号
							countInout.value = value + 1;
							getSubtotal(this);
							break;
							alert(countInout.value);
						case 'reduce': //点击了减号
							if(value > 1) {
								countInout.value = value - 1;
								getSubtotal(this);
							}
							break;
					}
					getSubtotal(this);
		
		
				}

		// 给数目输入框绑定keyup事件
		tr[i].getElementsByTagName('input')[0].onkeyup = function() {
			var val = parseInt(this.value);
			if(isNaN(val) || val <= 0) {
				val = 1;
			}
			if(this.value != val) {
				this.value = val;
			}
			getSubtotal(this.parentNode.parentNode); //更新小计

		}
	}
}
